/**************************************************************************
 * @file usr_compare.h
 * @author 黑马小乌龟 (532352406@qq.com)
 * @brief 
 * @version 0.1
 * @date 2025-10-17
 * 
 * @copyright Copyright (c) 2025
 * 
 *************************************************************************/
#ifndef USR_COMPARE__H_H
#define USR_COMPARE__H_H

#ifdef __cplusplus
extern "C"{
#endif
#include<stdint.h>
#include<stdio.h>

#include "hc32_ll.h"
#include "ev_hc32f334_lqfp64_bsp.h"

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
/* unlock/lock peripheral */
#define EXAMPLE_PERIPH_WE               (LL_PERIPH_GPIO | LL_PERIPH_EFM | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU)
#define EXAMPLE_PERIPH_WP               (LL_PERIPH_EFM | LL_PERIPH_FCG)

#define DAC_TEST_UNIT      (CM_DAC1)
#define DAC_CH             (DAC_CH1)
#define DAC_PERIP_CLK      (FCG3_PERIPH_DAC1)
#define DAC_REF_VOL        (2800ul /*mV*/)
#define CURR_AMP_RATIO     (34)      /*运放放大34倍*/
#define CURR_SAMPLE_RES    (0.01f)   /*采用电阻10mR*/
#define USR_DAC_RESOLUTION (1 << 12) /*12bit*/

#define DAC_VOL_0P2V                    (0x1000U / 28U * 2U)

/* Write Data  V = (Conversion Data / 4096) * AVCC */
#define GET_VOLTAGE_VALUE_BY_CURRENT(curr /*mA*/) (curr * CURR_SAMPLE_RES * CURR_AMP_RATIO)
#define GET_OCP_DAC_VALUE(curr /*mA*/)            (USR_DAC_RESOLUTION * GET_VOLTAGE_VALUE_BY_CURRENT(curr) / DAC_REF_VOL)
#define DEFAULT_OCP_DAC_VALUE                     GET_OCP_DAC_VALUE(3000ul) /*mA*/
#define DAC_DATA_ALIGN                            (DAC_DATA_ALIGN_RIGHT)

#define CMP_TEST_UNIT                   (CM_CMP1)
#define CMP_PERIP_CLK                   (FCG3_PERIPH_CMP1_2)
/* Define port and pin for CMP */
/* CMP1 compare voltage CMP1_INP2(PA3) */
#define CMP1_INP1_PORT                  (GPIO_PORT_A)
#define CMP1_INP1_PIN                   (GPIO_PIN_03)
/* CMP1_OUT */
#define CMP1_OUT_PORT                   (GPIO_PORT_A)
#define CMP1_OUT_PIN                    (GPIO_PIN_06)
#define CMP1_OUT_FUNC                   (GPIO_FUNC_2)

int32_t usr_compare_init(void);

#ifdef __cplusplus
}
#endif

#endif //USR_COMPARE__H_H


